<?php
/**
 * Author: 孙武 QQ:1228746736
 * Date: 2017/11/28
 * Time: 19:53
 */

$xmlData = '<xml><ToUserName><![CDATA[gh_99e41fac6e1a]]></ToUserName>
<FromUserName><![CDATA[oMaQa1SRXI3sx2dCYq5xDKmrDLyg]]></FromUserName>
<CreateTime>1511874345</CreateTime>
<MsgType><![CDATA[event]]></MsgType>//
<Event><![CDATA[subscribe]]></Event>//
<EventKey><![CDATA[qrscene_17]]></EventKey>
<Ticket><![CDATA[gQEY8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVUlacHhwTkFjcWwxMDAwME0wM2EAAgTskBpaAwQAAAAA]]></Ticket>
</xml>';


//$xmlArr= json_decode(json_encode(simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$xmlObj = simplexml_load_string($xmlData, 'SimpleXMLElement', LIBXML_NOCDATA);

/*
 * SimpleXMLElement Object
(
    [ToUserName] => gh_99e41fac6e1a
    [FromUserName] => oMaQa1W7E5TrJS1fmdGBso81NLoE
    [CreateTime] => 1511870676
    [MsgType] => event
    [Event] => SCAN
    [EventKey] => 48
    [Ticket] => gQHt8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydzZoQXhDTkFjcWwxMDAwMGcwM1AAAgQpshxaAwQAAAAA
)
 */

/**
 *  ToUserName    开发者微信号
 * FromUserName    发送方帐号（一个OpenID）
 * CreateTime    消息创建时间 （整型）
 * MsgType    消息类型，event
 * Event    事件类型，subscribe
 * EventKey    事件KEY值，qrscene_为前缀，后面为二维码的参数值
 * Ticket    二维码的ticket，可用来换取二维码图片
 */
$usrOpenId = $xmlObj->FromUserName;
$msgType = $xmlObj->MsgType;
$event = $xmlObj->Event;
$eventKey = $xmlObj->EventKey;
$msgType = $xmlObj->MsgType;
$eventKeyArr = explode('_', $eventKey);
$sence = $eventKeyArr[0];
$userScanId = $eventKeyArr[1];


if ($msgType == 'event' && $event == 'subscribe' && $sence == 'qrscene') {
    echo $usrOpenId;
    echo $userScanId;
}